package ui.panel
{
	import flash.display.Bitmap;

	import globals.ButtonType;
	import globals.Global;

	import ui.component.GameTextField;
	import ui.component.TextButton;

	import util.TextStyle;
	import util.UIUtil;

	/**
	 * 弹出提示框
	 * @author luguobin
	 */
	public class Alert extends BasePanel
	{
		private var _textField:GameTextField;
		private var _okBtn:TextButton;
		private var _cancelBtn:TextButton;
		public var okHandler:Function;
		public var cancelHandler:Function;

		public function Alert()
		{
			super();
			pid = PanelType.ALERT_PANEL;
			isMask = true;
			updateTitle("友好提示");
		}

		override protected function updateBg():void
		{
			_bg.bitmapData = Global.assetsManager.getAssetsAsBitmapData("alertPanel_bg");
		}

		override protected function initUI():void
		{
			this.addChild(_bg);
			updateBg();

			_titleBg = new Bitmap();
			_titleBg.bitmapData = Global.assetsManager.getAssetsAsBitmapData("alertPanel_title_bg");
			_titleBg.x = _bg.x + (_bg.width - _titleBg.width) * 0.5;
			_titleBg.y = _bg.y - 23;
			this.addChild(_titleBg);

			_title = new GameTextField();
			_title.defaultTextFormat = TextStyle.getTextFormat(0x653011, 20, true, "center");
			_title.x = _titleBg.x;
			_title.y = _bg.y - 15;
			TextStyle.addTextFrame(_title);
			_title.width = _titleBg.width;
			this.addChild(_title);

			_textField = new GameTextField();
			_textField.defaultTextFormat = TextStyle.getTextFormat();
			_textField.x = 72;
			_textField.y = 33;
			_textField.width = 245;
			this.addChild(_textField);

			_okBtn = UIUtil.getTextButton(0, "确定", ButtonType.BUTTON_GREEN_HEIGHT, btnHandler);
			_okBtn.x = 130;
			_okBtn.y = 75;
			this.addChild(_okBtn);

			_cancelBtn = UIUtil.getTextButton(1, "取消", ButtonType.BUTTON_BLUE_HEIGHT, btnHandler);
			_cancelBtn.x = 200;
			_cancelBtn.y = 75;
			this.addChild(_cancelBtn);
		}

		private function btnHandler(button:TextButton):void
		{
			if(button.id == 0)
			{
				if(okHandler != null)
				{
					okHandler();
				}
			}
			else if(button.id == 1)
			{
				if(cancelHandler != null)
				{
					cancelHandler();
				}
			}
			close();
		}

		public function set text(value:String):void
		{
			_textField.myHtmlText = value;
		}

		public function updateBtns(okBtnText:String = "确定", cancelBtnText:String = "取消"):void
		{
			_okBtn.text = okBtnText;
			_cancelBtn.text = cancelBtnText;
		}

		public static function show(text:String = "", title:String = "", okHandler:Function = null, cancelHandler:Function = null, okLabel:String = "确定",
			noLabel:String = "取消"):void
		{
			Global.uiManager.alert.updateBtns(okLabel, noLabel);
			Global.uiManager.alert.text = text;
			Global.uiManager.alert.updateTitle(title);
			Global.uiManager.alert.okHandler = okHandler;
			Global.uiManager.alert.cancelHandler = cancelHandler;
			Global.uiManager.openPanel(PanelType.ALERT_PANEL);
		}
	}
}